using System;
using UnityEngine;

namespace DanielSig.UnityExtensions
{
	public static class XColor
	{
		public static Color SetAlpha(this Color c, float a)
		{
			c.a = a;
			return c;
		}
		public static Color SetRGBA(this Color c, float r, float g, float b, float a)
		{
			c.r = r;
			c.g = g;
			c.b = b;
			c.a = a;
			return c;
		}
		public static Color SetRGB(this Color c, float r, float g, float b)
		{
			c.r = r;
			c.g = g;
			c.b = b;
			return c;
		}
		public static Color SetRed(this Color c, float r)
		{
			c.r = r;
			return c;
		}
		public static Color SetGreen(this Color c, float g)
		{
			c.g = g;
			return c;
		}
		public static Color SetBlue(this Color c, float b)
		{
			c.b = b;
			return c;
		}
		
		
		public static Color AddAlpha(this Color c, float a)
		{
			c.a += a;
			return c;
		}
		public static Color AddRGBA(this Color c, float r, float g, float b, float a)
		{
			c.r += r;
			c.g += g;
			c.b += b;
			c.a += a;
			return c;
		}
		public static Color AddRGB(this Color c, float r, float g, float b)
		{
			c.r += r;
			c.g += g;
			c.b += b;
			return c;
		}
		public static Color AddRed(this Color c, float r)
		{
			c.r += r;
			return c;
		}
		public static Color AddGreen(this Color c, float g)
		{
			c.g += g;
			return c;
		}
		public static Color AddBlue(this Color c, float b)
		{
			c.b += b;
			return c;
		}
		public static Color ScaleAlpha(this Color c, float a)
		{
			c.a *= a;
			return c;
		}
		public static Color ScaleRGBA(this Color c, float r, float g, float b, float a)
		{
			c.r *= r;
			c.g *= g;
			c.b *= b;
			c.a *= a;
			return c;
		}
		public static Color ScaleRGB(this Color c, float r, float g, float b)
		{
			c.r *= r;
			c.g *= g;
			c.b *= b;
			return c;
		}
		public static Color ScaleRed(this Color c, float r)
		{
			c.r *= r;
			return c;
		}
		public static Color ScaleGreen(this Color c, float g)
		{
			c.g *= g;
			return c;
		}
		public static Color ScaleBlue(this Color c, float b)
		{
			c.b *= b;
			return c;
		}
		public static Color Normalized(this Color c)
		{
			float max = 0F;
			for(int i = 0; i < 3; i++)
				if(c[i] > max) max = c[i];
			max = 1F / max;
			return c * new Color(max, max, max, 1F);
		}
		public static Color Saturated(this Color c)
		{
			float max = 0F;
			for(int i = 0; i < 3; i++)
				if(c[i] > max) max = c[i];
			max = 1F-max;
			return c + new Color(max, max, max, 0F);
		}
		public static Color Hue(this Color c)
		{
			float max = 0F;
			float min = 1F;
			for(int i = 0; i < 3; i++)
			{
				if(c[i] > max) max = c[i];
				if(c[i] < min) min = c[i];
			}
			max = 1F / (max - min);
			return (c - new Color(min, min, min, 0F)) * new Color(max, max, max, 1F);
		}
		public static Color GetThermalScale(float heat)
		{
			Color c = Color.white;
			heat = Mathf.Clamp01(heat);
			float redHeat = Mathf.Cos(heat*1.64F);
			c.r = 1 - redHeat * redHeat * redHeat * redHeat;
			c.r = heat * heat * heat;
			c.b = Mathf.Sin(heat * 6.8068F) * 0.5F + heat * 0.75F;
			return c;
		}
	}
}

